package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.MyDiscountCouponActivity;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter;
import com.kkd.kkdapp.http.entity.MyDiscountCouponData;
import com.kkd.kkdapp.http.entity.MyDiscountCouponDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;

import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class MyDiscountCouponFragment extends Fragment implements
		KKDScrollListener {

	private MyDiscountCouponActivity mMyDiscountCouponActivity;
	private ImageView mNullImg;
	private TextView mNullTv;
	private KkdXlistView manage_lv;
	private int status;
	// private String version;
	private int pageNO = 1;
	private int pageSize = 10;
	private List<MyDiscountCouponDataList> mList;
	private boolean REFRESHFLAG = true;
	private MyDiscountCouponAdapter adapter;
	private String couponType = "";
	private ListView mListView;
	Toast mToast;

	public static MyDiscountCouponFragment newInstance(int status,
			String couponType) {
		MyDiscountCouponFragment fragment = new MyDiscountCouponFragment();
		Bundle mBundle = new Bundle();
		mBundle.putInt("status", status);
		mBundle.putString("couponType", couponType);
		fragment.setArguments(mBundle);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mMyDiscountCouponActivity = (MyDiscountCouponActivity) activity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentView = inflater.inflate(R.layout.mydiscountcoupon_fragment,
				null);
		couponType = getArguments().getString("couponType");
		status = getArguments().getInt("status");
		manage_lv = (KkdXlistView) contentView
				.findViewById(R.id.deal_detail_list);
		if (mList == null) {
			mList = new ArrayList<MyDiscountCouponDataList>();
		}

		manage_lv.setKKDScrollListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		adapter = new MyDiscountCouponAdapter(getActivity(), false, mList, -3,
				"");
		mListView.setAdapter(adapter);
		manage_lv.startRefresh();

		mNullImg = (ImageView) contentView.findViewById(R.id.id_no_content);
		mNullTv = (TextView) contentView.findViewById(R.id.id_no_content_text);
		if (AppUtil.isConnect(getActivity())) {
			couponList(status);
			REFRESHFLAG = true;
		} else {
			if (mList == null) {
				mNullImg.setVisibility(View.VISIBLE);
				mNullTv.setVisibility(View.VISIBLE);
				if ("".equals(couponType)) {
					mNullTv.setText("没有相对应的优惠券 ");
				} else if ("1".equals(couponType)) {
					mNullTv.setText("暂无抵现券");
				} else if ("2".equals(couponType)) {
					mNullTv.setText("暂无加息券");
				} else if ("3".equals(couponType)) {
					mNullTv.setText("暂无返现券");
				} else if ("4".equals(couponType)) {
					mNullTv.setText("暂无提现券");
				}
				// manage_lv.setPullLoadEnable(false);
			} else {
				// manage_lv.setPullLoadEnable(true);
				mNullImg.setVisibility(View.GONE);
				mNullTv.setVisibility(View.GONE);
			}
			manage_lv.stopRefresh();
		}
		// manage_lv.setXListViewListener(this);
		// manage_lv.setPullRefreshEnable(true);
		return contentView;
	}

	/**
	 * 优惠券
	 */
	public void couponList(int status) {

		KkdHttpMagager.getInstance().getMyDiscountManager("", couponType, "",
				"", pageNO, pageSize, status, 
				MyDiscountCouponData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyDiscountCouponData mMyDiscountCouponData = (MyDiscountCouponData) responseBody;
						int pageNO = mMyDiscountCouponData.getPageNO();
						int pageCount = mMyDiscountCouponData.getPageCount();
						if (pageNO > pageCount) {
							manage_lv.stopLoadMore(-1);
							if (REFRESHFLAG) {
								manage_lv.stopLoadMore(1);
							}
							manage_lv.stopRefresh();
						} else {
							manage_lv.setNoMore(false);
							if (pageNO == 1 && mList != null) {
								mList.clear();
							}
							mList.addAll(mMyDiscountCouponData.getDataList());
							// adapter.appendData(mList, pageNO == 1);
							// adapter.update();
							adapter.notifyDataSetChanged();
							if (mMyDiscountCouponData.getDataList().size() < pageSize) {
								manage_lv.stopLoadMore(1);
							} else {
								manage_lv.stopLoadMore(0);
							}
							manage_lv.stopRefresh();
							Time_Date time_Date = new Time_Date();
							String time = time_Date.get_Year_Day();
							manage_lv.setRefreshTime(time);
						}
						if (REFRESHFLAG) {
							if (mList == null) {
								if (mList.size() == 0) {
									mNullImg.setVisibility(View.VISIBLE);
									mNullTv.setVisibility(View.VISIBLE);
									if ("".equals(couponType)) {
										mNullTv.setText("没有相对应的优惠券");
									} else if ("1".equals(couponType)) {
										mNullTv.setText("暂无抵现券");
									} else if ("2".equals(couponType)) {
										mNullTv.setText("暂无加息券");
									} else if ("3".equals(couponType)) {
										mNullTv.setText("暂无返现券");
									} else if ("4".equals(couponType)) {
										mNullTv.setText("暂无提现券");
									}
									// manage_lv.setPullLoadEnable(false);
								}
							} else {
								if (mList.size() == 0) {
									mNullImg.setVisibility(View.VISIBLE);
									mNullTv.setVisibility(View.VISIBLE);
									if ("".equals(couponType)) {
										mNullTv.setText("没有相对应的优惠券");
									} else if ("1".equals(couponType)) {
										mNullTv.setText("暂无抵现券");
									} else if ("2".equals(couponType)) {
										mNullTv.setText("暂无加息券");
									} else if ("3".equals(couponType)) {
										mNullTv.setText("暂无返现券");
									} else if ("4".equals(couponType)) {
										mNullTv.setText("暂无提现券");
									}
									// manage_lv.setPullLoadEnable(false);
								} else if (mList.size() > 0) {
									// manage_lv.setPullLoadEnable(true);
									mNullImg.setVisibility(View.GONE);
									mNullTv.setVisibility(View.GONE);
								}
							}
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						if (REFRESHFLAG) {
							if (mList == null) {
								if (mList.size() == 0) {
									mNullImg.setVisibility(View.VISIBLE);
									mNullTv.setVisibility(View.VISIBLE);
									if ("".equals(couponType)) {
										mNullTv.setText("没有相对应的优惠券");
									} else if ("1".equals(couponType)) {
										mNullTv.setText("暂无抵现券");
									} else if ("2".equals(couponType)) {
										mNullTv.setText("暂无加息券");
									} else if ("3".equals(couponType)) {
										mNullTv.setText("暂无返现券");
									} else if ("4".equals(couponType)) {
										mNullTv.setText("暂无提现券");
									}
									// manage_lv.setPullLoadEnable(false);
								}
							} else {
								if (mList.size() == 0) {
									mNullImg.setVisibility(View.VISIBLE);
									mNullTv.setVisibility(View.VISIBLE);
									if ("".equals(couponType)) {
										mNullTv.setText("没有相对应的优惠券");
									} else if ("1".equals(couponType)) {
										mNullTv.setText("暂无抵现券");
									} else if ("2".equals(couponType)) {
										mNullTv.setText("暂无加息券");
									} else if ("3".equals(couponType)) {
										mNullTv.setText("暂无返现券");
									} else if ("4".equals(couponType)) {
										mNullTv.setText("暂无提现券");
									}
									// manage_lv.setPullLoadEnable(false);
								} else if (mList.size() > 0) {
									// manage_lv.setPullLoadEnable(true);
									mNullImg.setVisibility(View.GONE);
									mNullTv.setVisibility(View.GONE);
								}
							}
						}
					}
				});

		// KkdHttpMagager.getInstance().getMyDiscountManager("", couponType, "",
		// "", SystemUtil.getVersionName(mMyDiscountCouponActivity),
		// cat[1], pageNO, pageSize, status, cat[0],
		// MyDiscountCoupon.class, new HttpCallBackT() {
		//
		// @Override
		// public void onSuccess(int statusCode, Object res) {
		// MyDiscountCoupon mDiscountCoupon = (MyDiscountCoupon) res;
		// if (mDiscountCoupon.getCode() == 0) {
		// MyDiscountCouponData mMyDiscountCouponData = mDiscountCoupon
		// .getData();
		// int pageNO = mMyDiscountCouponData.getPageNO();
		// int pageCount = mMyDiscountCouponData
		// .getPageCount();
		// if (pageNO > pageCount) {
		// manage_lv.stopLoadMore(-1);
		// } else {
		// if (pageNO == 1 && mList != null) {
		// mList.clear();
		// }
		// mList.addAll(mMyDiscountCouponData
		// .getDataList());
		// // adapter.appendData(mList, pageNO == 1);
		// // adapter.update();
		// adapter.notifyDataSetChanged();
		// manage_lv.stopLoadMore(0);
		// manage_lv.stopRefresh();
		// Time_Date time_Date = new Time_Date();
		// String time = time_Date.get_Year_Day();
		// manage_lv.setRefreshTime(time);
		// }
		// } else if (mDiscountCoupon.getCode() == -2) {
		// } else {
		// Toasttool.MyToast(mMyDiscountCouponActivity,
		// mDiscountCoupon.getDesc());
		// }
		// if (REFRESHFLAG) {
		// if (mList == null) {
		// if (mList.size() == 0) {
		// mNullImg.setVisibility(View.VISIBLE);
		// mNullTv.setVisibility(View.VISIBLE);
		// if ("".equals(couponType)) {
		// mNullTv.setText("暂无优惠券");
		// } else if ("1".equals(couponType)) {
		// mNullTv.setText("暂无抵现券");
		// } else if ("2".equals(couponType)) {
		// mNullTv.setText("暂无加息券");
		// } else if ("3".equals(couponType)) {
		// mNullTv.setText("暂无返现券");
		// } else if ("4".equals(couponType)) {
		// mNullTv.setText("暂无提现券");
		// }
		// manage_lv.setPullLoadEnable(false);
		// }
		// } else {
		// if (mList.size() == 0) {
		// mNullImg.setVisibility(View.VISIBLE);
		// mNullTv.setVisibility(View.VISIBLE);
		// if ("".equals(couponType)) {
		// mNullTv.setText("暂无优惠券");
		// } else if ("1".equals(couponType)) {
		// mNullTv.setText("暂无抵现券");
		// } else if ("2".equals(couponType)) {
		// mNullTv.setText("暂无加息券");
		// } else if ("3".equals(couponType)) {
		// mNullTv.setText("暂无返现券");
		// } else if ("4".equals(couponType)) {
		// mNullTv.setText("暂无提现券");
		// }
		// manage_lv.setPullLoadEnable(false);
		// } else if (mList.size() > 0) {
		// manage_lv.setPullLoadEnable(true);
		// mNullImg.setVisibility(View.GONE);
		// mNullTv.setVisibility(View.GONE);
		// }
		// }
		// }
		// }
		//
		// @Override
		// public void onFailure(int statusCode, byte[] responseBody,
		// Throwable error) {
		// if (REFRESHFLAG) {
		// if (mList == null) {
		// if (mList.size() == 0) {
		// mNullImg.setVisibility(View.VISIBLE);
		// mNullTv.setVisibility(View.VISIBLE);
		// if ("".equals(couponType)) {
		// mNullTv.setText("暂无优惠券");
		// } else if ("1".equals(couponType)) {
		// mNullTv.setText("暂无抵现券");
		// } else if ("2".equals(couponType)) {
		// mNullTv.setText("暂无加息券");
		// } else if ("3".equals(couponType)) {
		// mNullTv.setText("暂无返现券");
		// } else if ("4".equals(couponType)) {
		// mNullTv.setText("暂无提现券");
		// }
		// manage_lv.setPullLoadEnable(false);
		// }
		// } else {
		// if (mList.size() == 0) {
		// mNullImg.setVisibility(View.VISIBLE);
		// mNullTv.setVisibility(View.VISIBLE);
		// if ("".equals(couponType)) {
		// mNullTv.setText("暂无优惠券");
		// } else if ("1".equals(couponType)) {
		// mNullTv.setText("暂无抵现券");
		// } else if ("2".equals(couponType)) {
		// mNullTv.setText("暂无加息券");
		// } else if ("3".equals(couponType)) {
		// mNullTv.setText("暂无返现券");
		// } else if ("4".equals(couponType)) {
		// mNullTv.setText("暂无提现券");
		// }
		// manage_lv.setPullLoadEnable(false);
		// } else if (mList.size() > 0) {
		// manage_lv.setPullLoadEnable(true);
		// mNullImg.setVisibility(View.GONE);
		// mNullTv.setVisibility(View.GONE);
		// }
		// }
		// }
		// }
		// });

	}

	Handler handler = new Handler();

	@Override
	public void onRefresh() {
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				pageNO = 1;
				couponList(status);
				manage_lv.stopRefresh();
				REFRESHFLAG = true;
			}
		}, 1000);
	}

	@Override
	public void onLoadMore() {
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				pageNO++;
				REFRESHFLAG = false;
				couponList(status);
			}
		}, 1000);
	}

	public void showToast(String hintLanguage) {
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
		mToast = Toast.makeText(mMyDiscountCouponActivity, hintLanguage,
				Toast.LENGTH_SHORT);
		mToast.show();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}
	// @Override
	// public void onRefresh() {
	// handler.postDelayed(new Runnable() {
	//
	// @Override
	// public void run() {
	// pageNO = 1;
	// couponList(status);
	// manage_lv.stopRefresh();
	// REFRESHFLAG = true;
	// }
	// }, 1000);
	// }
	//
	// @Override
	// public void onLoadMore() {
	// handler.postDelayed(new Runnable() {
	//
	// @Override
	// public void run() {
	// pageNO++;
	// REFRESHFLAG = false;
	// couponList(status);
	// }
	// }, 1000);
	//
	// }
}
